package br.com.studyLife.interceptor;

import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.resource.ResourceMethod;
import br.com.studyLife.dao.AlertaDao;
import br.com.studyLife.dao.RelacionamentoDao;
import br.com.studyLife.modelo.Usuario;
import br.com.studyLife.modelo.UsuarioWeb;

@Intercepts(after=AutorizacaoInterceptor.class)
public class MeuPerfilInterceptor implements Interceptor {

	private final UsuarioWeb usuarioWeb;
	private final AlertaDao alertaDao;
	private final RelacionamentoDao relacionamentoDao;
	private final Result result;
	
	public MeuPerfilInterceptor(UsuarioWeb usuarioWeb, AlertaDao alertaDao, RelacionamentoDao relacionamentoDao, Result result) {
		this.usuarioWeb = usuarioWeb;
		this.alertaDao = alertaDao;
		this.relacionamentoDao = relacionamentoDao;
		this.result = result;
	}
	
	@Override
	public boolean accepts(ResourceMethod method) {
		return method.containsAnnotation(MeuPerfil.class);
	}

	@Override
	public void intercept(InterceptorStack stack, ResourceMethod method,
			Object resourceInstance) throws InterceptionException {
		
		Usuario usuario = usuarioWeb.getUser();
		result.include("totalAlertas", alertaDao.totalAlertas(usuario));
		result.include("amigos", relacionamentoDao.amigosDe(usuario));
		stack.next(method, resourceInstance);
		
	}

}
